<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script type="text/javascript">
    //全局变量
    var currentTab="0";
    var clinicType="0";
    $(function(){
        heightWidthDoctor();
        function heightWidthDoctor(){
            var bodyHeight=$("body").height();
            var footerHeight=$("#footerId").height();
            var headerHeight=$("#headerId").height();
            var patientDivHeight=$("#patientDiv").height();
            var d1=$("#d1").height();
            var d2=$("#d2").height();
            var d3=$("#tags").height();
            var d4=$("#footSearchId").height();
            $("#resultNav").height(patientDivHeight-d1-d2-d3-d4-60);

            var doctorUl=$("#doctorUlId").height();
            $("#rigthDoctorCenterDiv").height(bodyHeight-footerHeight-headerHeight-doctorUl);
        }
        $("#patientUl").height($("#patientDiv").height()*0.76);
        initTree();
        $("#searchBtn").on('click',function(){
            queryPatientList("0");
        })
        $("#resultNav").scroll(function(){
            var divHeight = $(this).height();
            var nScrollHeight = $(this)[0].scrollHeight;
            var nScrollTop = $(this)[0].scrollTop;
            if(nScrollTop + divHeight >= nScrollHeight) {
                //请求数据
                queryPatientList("1");
            }
        });
        queryPatientList();
      })

    /**
     * 切换我的患者 全科患者
     * */
    function selectTag(type,selfObj){
        $(selfObj).parent().parent().find("li").removeClass("selectTag")
        $(selfObj).parent().addClass("selectTag");
        if(type=='0') {
            $("#deptBtn").attr("disabled","disabled");
        }else if(type=='1'){
            $("#deptBtn").attr("disabled",false);//将科室选择ztree可以选择
        }
        queryPatientList("0");
    }
     /*
     * 加载病人列表
     *
     * */
    function queryPatientList(plus){
        var pageNo=$("#pageNoPatient").val();
        if(plus=="0"){
            pageNo=1;
            $("#resultShow").html("");
        }
        var ident=$("#tags .selectTag").attr("ident");
        var clinicType=$("input[name='clinicType']:checked").val();
        var deptId="";
        var deptName="";
        var powerDeptIds="";
        var likeText=$("#likeText").val();
        var pageSize=$("#pageSizePatient").val();
        deptId=$("#deptId").val();
        deptName=$("#deptNameId").val();
        if(ident=="1"){
            powerDeptIds="1";
        }
        var requestParams= {pageNo:pageNo,pageSize:pageSize,powerDeptIds:powerDeptIds,clinicType:clinicType,likeText:likeText,deptId:deptId,deptName:deptName};
        $.ajax({
            type: "POST",
            data:requestParams,
            url: "${ctx}/oper/clinicMaster/findPatientByConditions",
            dataType: "json",
            success: function (data) {
                if(data.list!=null && data.list!=''){
                    if(data.list.length>0){
                        $("#resultShow").append(appendDataStr(data));
                        if(plus=="0"){
                            $("#resultShow li:first").click();
                        }
                        //将下次查找的起始索引位置写到隐藏域
                        $("#countAmount").html(data.count+"人");
                        $("#pageSizePatient").val(data.pageSize);
                        $("#pageNoPatient").val(Number(data.pageNo)+1);
                    }
                }
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }

    /**
     *  拼接返回的json列表
     * @param data   json 字符串
     */
    function appendDataStr(data){
        var returnStr="";
        for(var i=0;i<data.list.length;i++){
             returnStr+="<li onclick=setPatientValue('"+data.list[i].patientId+"','"+data.list[i].id+"','"+data.list[i].doctorId+"',this)><a><div class='clearfix' style='padding-right:5px;'><span class='pull-left'>"+data.list[i].name+"</span><span class='pull-right'>("+data.list[i].sex+")&nbsp;&nbsp;"+data.list[i].age+"岁</span></div> <div class='clearfix' style='padding-right:5px;'><span>门诊号："+data.list[i].clinicNo;
            if(data.list[i].referralNum==0 && (data.list[i].doctorId==null || data.list[i].doctorId=='')){
                returnStr+="<button class='btn btn-info pull-right btn-xs' onclick=\"referral(\'"+data.list[i].id+"\')\">转诊</button>";
            }
            returnStr+="</span></div></a></li>";
        }
        return returnStr;
    }
    /**
     * 转诊科室
     * */
    function referral(id){
        var url='${ctx}/tag/treeselect?url='+encodeURIComponent("/sys/orgDept/treeData?outpOrInps=0,2")+'&selected=true&id=Referral';
        url=timestamp(url);
        layerReferral=layer.open({
            title:'请选择转诊科室',
            type: 2,
            area: ['23%', '75%'],
            fixed: false, //不固定
            maxmin: true,
            content: url,
            btn: ['确定', '取消'],
            yes:function(index,layero){
                var tree =$(layero).find("iframe")[0].contentWindow.tree;
                okReferralClinic(tree,id);
            } ,no:function(index){
                layer.close(index);
            }
        });

    }
    /**
     * 选择科室后触发事件
     * */
    function okReferralClinic(tree,id){
        var ids = "", names = [], nodes = [];
        nodes = tree.getSelectedNodes();
        ids=nodes[0].id;
        if(flag){
            flag=false;
            $.ajax({
                type: "POST",
                url: "${ctx}/oper/clinicMaster/updateReferral",
                data:{id:id,deptId:ids},
                dataType: "json",
                success: function (data) {
                    if (data.code == "success") {
                        toastr.success(data.data);
                        layer.close(layerReferral);
                        queryPatientList('0');
                    } else {
                        flag = true;
                        toastr.error(data.data);
                    }
                },
                error: function (data) {
                    toastr.error('网络连接错误,请检查网络');
                }
            });
        }
    }
    /**
     * 选择病人
     * */
    function setPatientValue(patientId,clinicId,doctorId,li){
        $("#patientIdVal").val(patientId);
        $("#doctorIdVal").val(doctorId);
        $("#clinicIdVal").val(clinicId);
        $(li).parent().find("li").attr("class","");
        $(li).attr("class","selected");
        $("#doctorUlId li:first a:first").click();
    }


    function initTree(){
        var setting = {data:{simpleData:{enable:true,idKey:"id",pIdKey:"pId",rootPId:'0'}},
            callback:{onClick:function(event, treeId, treeNode){
                var id = treeNode.pId == '0' ? '' :treeNode.pId;
                $("#deptNameId").val(treeNode.name);//显示值
                $("#deptId").val(treeNode.id);//显示值
                //加载选择科室的数据
                queryPatientList("0");
            }
            }
        };
        $.getJSON("${ctx}/sys/orgDept/treeData?role=1",function(data){
            $.fn.zTree.init($("#ztreePatient"), setting, data).expandAll(true);
        });

     }


    /**
    * 切换医生操作界面
    * @param url
    * @param li
     */
    function loadDoctorDiv(url,li){
        var patientId=$("#patientIdVal").val();
        var clinicId=$("#clinicIdVal").val();
        if(checkParams(patientId,clinicId,li)){
            url=url+"?patientId="+patientId+"&clinicId="+clinicId+"&clinicAttr=a&doctorOper=0";
            loadDiv('rigthDoctorCenterDiv',url);
        }else{
            toastr.error("请先选择病人！");
        }
     }
    //校验参数是否获取到
    function checkParams(patientId,clinicId,selfObj){
        if(patientId==null || patientId==""){
            toastr.error("请先选择病人!");
            return false;
        }
        if(clinicId==null || clinicId==""){
            toastr.error("获取病人就诊记录id失败！");
            return false;
        }
        $(selfObj).parent().parent().find("li").attr("class","");
        $(selfObj).parent().attr("class","active");
        return true;
    }
    //1:待诊为已诊，病人变为我的病人，2只有病人变为我的病人
//    function updateClinicTypeOrMyPat(type){
//        var clinicId=$("#clinicIdVal").val();
//        var doctorId=$("#doctorIdVal").val();
//        var clinicType="1";
        <%--if(type=="1"){--%>
            <%--var requestParams= {id:clinicId,clinicType:clinicType,doctorId:doctorId};--%>
        <%--}--%>
        <%--else if(type=="2"){--%>
            <%--var requestParams= {id:clinicId,doctorId:doctorId};--%>
        <%--}--%>
        <%--$.ajax({--%>
            <%--type: "POST",--%>
            <%--data:requestParams,--%>
            <%--url: "${ctx}/oper/clinicMaster/updateClinicTypeOrMyPat",--%>
            <%--dataType: "json",--%>
            <%--success: function (data) {--%>
               <%--/* queryPatientList("0");*/--%>
            <%--},--%>
            <%--error: function (data) {--%>
                <%--toastr.error('网络连接错误,请检查网络');--%>
            <%--}--%>
        <%--});--%>
//    }
</script>
<div class="app">
    <section class="layout">
        <!-- sidebar menu -->

        <!-- /sidebar menu -->

        <!-- mail sidebar navigation -->
        <aside class="sidebar-230 canvas-right bg-default ">
            <header class="header navbar clearfix bb bg-default pl0 pr0" id="innerHead">
                <p class="navbar-text">患者列表</p>
                <div class="btn-group pull-right mr10">
                    <button type="button" class="btn btn-sm btn-primary navbar-btn" onclick="queryPatientList('0')">
                        <i class="fa fa-refresh"></i>刷新列表
                    </button>
                 </div>
             </header>
             <div class="content-wrap no-p">

                <div class="wrapper" id="patientDiv">
                    <div class="choice-wrap" id="d1">
                        <input type="radio" name="clinicType" onclick="queryPatientList('0')"  checked="checked" value="0" >待诊
                        <input type="radio" name="clinicType"onclick="queryPatientList('0')"   value="1" >已诊
                        <span class="badge pull-right bg-success" id="countAmount"></span>
                    </div>
                    <div id="d2">
                        <div class="input-group" style="margin-top: 0px;margin-bottom: 2px">
                            <input type="hidden" id="deptId" value="${fns:getUser().orgDept.id}">
                            <input  id="deptNameId" type="text" class="form-control" value="${fns:getUser().orgDept.deptName}" disabled="true">
                            <div class="input-group-btn">
                                <button id="deptBtn" type="button"  class="btn btn-default dropdown-toggle" data-toggle="dropdown" tabindex="-1" aria-expanded="false">
                                    <span class="caret"></span>
                                </button>
                                    <div  class="dropdown-menu pull-right" role="menu" >
                                        <div class="ztree" id="ztreePatient" style="min-width: 220px;max-width: 220px;overflow: auto">

                                        </div>
                                    </div>
                            </div>
                        </div>
                    </div>
                    <ul class="clearfix tags_new" id="tags">
                        <li class="selectTag" ident="0">
                            <A onClick="selectTag('0',this)"
                               href="javascript:void(0)">我的患者</A> </li>
                        <li ident="1"><A onClick="selectTag('1',this)"
                               href="javascript:void(0)">全科患者</A>  </li>
                        <li><A
                               href="javascript:void(0)">会诊患者</A> </li>
                    </ul>
                    <nav role="navigation" id="resultNav"  style="overflow:auto">
                        <div class="no-padding">
                            <ul class="nav nav-stacked" id="resultShow" >

                            </ul>
                            <%--<a id='loadMoreBtn' href="#">加载更多</a>--%>
                            <div class="foot-search" id="footSearchId" >
                                 <input type="text" class="sear-input" id="likeText"><button id='searchBtn' class="sear-btn">搜索</button>
                            </div>
                        </div>
                    </nav>
                </div>
            </div>
        </aside>
        <!-- /mail sidebar navigation -->

        <!-- main content -->
        <section class="main-content" id="doctorSectionId">
            <div class="content-wrap">
                <div class="wrapper"  >
                    <ul class="nav nav-tabs" id="doctorUlId">
                        <li class="active">
                            <a href="#" onclick="loadDoctorDiv('${ctx}/oper/patMasterIndex/findById',this)">基本信息</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/pregnantRecords/index',this)">孕妇手册</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/clinicRecord/index',this);" >病人病历</a>
                        </li>
                        <li id="examApplyLi">
                            <a href="#"  onclick="loadDoctorDiv('${ctx}/exam/examApply/index',this)">检查申请</a>
                        </li>
                        <li id="labApplyLi">
                            <a href="#" onclick="loadDoctorDiv('${ctx}/lab/labApply/index',this)">检验申请</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/prescriptionApply/index',this)" >处方申请</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/outpTreatRec/material',this)">卫生材料</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/outpTreatRec/index',this)">处置治疗</a>
                        </li>
                        <%--<li>--%>
                            <%--<a href="#" onclick="loadDoctorDiv('${ctx}/operation/operationApply/index',this)">手术申请</a>--%>
                        <%--</li>--%>
                        <%--<li>--%>
                            <%--<a href="#" onclick="loadDoctorDiv('${ctx}/doctor/prescriptionApply/index',this)">用血申请</a>--%>
                        <%--</li>--%>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/finance/outpRcptMaster/index',this)" >诊间收费</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/liveRequisition/index',this)" >住院通知单</a>
                        </li>
                        <li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/clinicDeathProve/index',this)" >死亡证明</a>
                        </li><li>
                            <a href="#" onclick="loadDoctorDiv('${ctx}/doctor/placeInfectionRecord/index',this)" >传染病</a>
                        </li>
                    </ul>
                    <div class="row bg-color" style="overflow: auto" id="rigthDoctorCenterDiv">

                    </div>
                </div>
            </div>
        </section>
        <!-- /main content -->
        <%--点击左侧患者patientId 隐藏域--%>
        <input type="hidden" id="patientIdVal" value=""/>
        <input type="hidden" id="doctorIdVal" value=""/>
        <input type="hidden" id="clinicIdVal" value=""/>
        <input type="hidden" id="pageNoPatient" value=""/>
        <input type="hidden" id="pageSizePatient" value=""/>
        <input type="hidden" id="requestParams" value=""/>
      </section>
</div>

